home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Text Editor / Source / ValueStream.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-13  |  7.7 KB  |  344 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ValueStream.cpp
  3.  
  4.     Contains:    Stream class implementation for Textension I/O
  5.  
  6.     Written by:    Steve Smith
  7.     
  8.     Copyright:    © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11.  
  12. #ifndef _VALUESTREAM_
  13. #include "ValueStream.h"
  14. #endif
  15.  
  16. // ----- SOM Includes -----
  17.  
  18. #include <som.xh>
  19. #include <somobj.xh>
  20.  
  21. // ----- OpenDoc Includes -----
  22.  
  23. #ifndef SOM_ODStorageUnitView_xh
  24. #include "SUView.xh"
  25. #endif
  26.  
  27. #ifndef SOM_Module_OpenDoc_Errors_defined
  28. #include <ErrorDef.xh>
  29. #endif
  30.  
  31. #ifndef __EXCEPT__
  32. #include <Except.h>
  33. #endif
  34.  
  35. // ----- OpenDoc Utilities -----
  36.  
  37. #ifndef _ODUTILS_
  38. #include <ODUtils.h>
  39. #endif
  40.  
  41. #ifndef _STORUTIL_
  42. #include <StorUtil.h>
  43. #endif
  44.  
  45. #pragma segment TextEditorUtils
  46.  
  47. //==============================================================================
  48. // CLASS SUValueStream
  49. //==============================================================================
  50.  
  51. //------------------------------------------------------------------------------
  52. // SUValueStream::SUValueStream
  53. //------------------------------------------------------------------------------
  54.  
  55. SUValueStream::SUValueStream() :
  56.     fView(NULL)
  57. {
  58. }
  59.     
  60. //------------------------------------------------------------------------------
  61. // SUValueStream::~SUValueStream
  62. //------------------------------------------------------------------------------
  63.  
  64. SUValueStream::~SUValueStream()
  65. {
  66. }
  67.  
  68. //------------------------------------------------------------------------------
  69. // SUValueStream::InitValueStream
  70. //------------------------------------------------------------------------------
  71.  
  72. void SUValueStream::InitValueStream(ODStorageUnitView *view)
  73. {
  74.     if ( fView )
  75.         // should be som_Free()
  76.         ODDeleteObject(fView);
  77.         
  78.     CStream::IStream();
  79.     fView = view;
  80. }
  81.  
  82. //------------------------------------------------------------------------------
  83. // SUValueStream::Free
  84. //------------------------------------------------------------------------------
  85.  
  86. void SUValueStream::Free()
  87. {
  88.     if ( this->ValidateStorageUnitView() )
  89.         ODDeleteObject(fView);
  90. }
  91.  
  92. //------------------------------------------------------------------------------
  93. // SUValueStream::WriteBytes
  94. //------------------------------------------------------------------------------
  95.  
  96. OSErr SUValueStream::WriteBytes(const void* theBytes, long bytesCount)
  97. {
  98.     if ( this->ValidateStorageUnitView() )
  99.     {
  100.         Environment* ev = somGetGlobalEnvironment();
  101.         
  102.         TRY
  103.             StorageUnitViewSetValue(fView, ev, bytesCount, (ODValue)theBytes);
  104.         CATCH_ALL
  105.             // We can't throw from a Textension class.
  106.             return ErrorCode();
  107.         ENDTRY
  108.         
  109.         return noErr;
  110.     }
  111.     
  112.     return kODErrInvalidStorageUnit;
  113. }
  114.  
  115. //------------------------------------------------------------------------------
  116. // SUValueStream::ReadBytes
  117. //------------------------------------------------------------------------------
  118.  
  119. OSErr SUValueStream::ReadBytes(void* theBytes, long bytesCount)
  120. {
  121.     if ( this->ValidateStorageUnitView() )
  122.     {
  123.         Environment* ev = somGetGlobalEnvironment();
  124.         TRY
  125.             StorageUnitViewGetValue(fView, ev, bytesCount, theBytes);
  126.         CATCH_ALL
  127.             // We can't throw from a Textension class.
  128.             return ErrorCode();
  129.         ENDTRY
  130.         
  131.         return noErr;
  132.     }
  133.     
  134.     return kODErrInvalidStorageUnit;
  135. }
  136.  
  137.  
  138. //------------------------------------------------------------------------------
  139. // SUValueStream::GetPosition
  140. //------------------------------------------------------------------------------
  141.  
  142. long SUValueStream::GetPosition() const
  143. {
  144.     if ( this->ValidateStorageUnitView() )
  145.     {
  146.         TRY
  147.             Environment* ev = somGetGlobalEnvironment();
  148.             return fView->GetOffset(ev);
  149.         CATCH_ALL
  150.             // We can't throw from a Textension class.
  151.         ENDTRY
  152.     }
  153.     
  154.     return 0;
  155. }
  156.  
  157. //------------------------------------------------------------------------------
  158. // SUValueStream::SetPosition
  159. //------------------------------------------------------------------------------
  160.  
  161. void SUValueStream::SetPosition(long newPosition)
  162. {
  163.     if ( this->ValidateStorageUnitView() )
  164.     {
  165.         TRY
  166.             Environment* ev = somGetGlobalEnvironment();
  167.         
  168.             char    padByte = 0x00;
  169.             long    numBytes = 0;
  170.             long    curSize = 0;
  171.             curSize = fView->GetSize(ev);
  172.         
  173.             if ( curSize < newPosition )
  174.                 numBytes = newPosition - curSize;
  175.         
  176.             fView->SetOffset(ev, curSize);
  177.             
  178.             for (int i = 0; i < numBytes; i++)
  179.             {
  180.                 StorageUnitViewSetValue(fView, ev, 1, &padByte);
  181.             }
  182.  
  183.             fView->SetOffset(ev, newPosition);
  184.         CATCH_ALL
  185.             // We can't throw from a Textension class.
  186.         ENDTRY
  187.     }
  188. }
  189.  
  190. //------------------------------------------------------------------------------
  191. // SUValueStream::Skip
  192. //------------------------------------------------------------------------------
  193.  
  194. void SUValueStream::Skip(long count)
  195. {
  196.     if ( this->ValidateStorageUnitView() )
  197.     {
  198.         TRY
  199.             Environment* ev = somGetGlobalEnvironment();
  200.             this->SetPosition(fView->GetOffset(ev) + count);
  201.         CATCH_ALL
  202.             // We can't throw from a Textension class.
  203.         ENDTRY
  204.     }
  205. }
  206.  
  207. //------------------------------------------------------------------------------
  208. // SUValueStream::GetSize
  209. //------------------------------------------------------------------------------
  210.  
  211. long SUValueStream::GetSize() const
  212. {
  213.     if ( this->ValidateStorageUnitView() )
  214.     {
  215.         TRY
  216.             Environment* ev = somGetGlobalEnvironment();
  217.             return fView->GetSize(ev);
  218.         CATCH_ALL
  219.             // We can't throw from a Textension class.
  220.         ENDTRY
  221.     }
  222.     
  223.     return 0;
  224. }
  225.  
  226. //------------------------------------------------------------------------------
  227. // SUValueStream::Append
  228. //------------------------------------------------------------------------------
  229.  
  230. OSErr SUValueStream::Append(long count)
  231. {
  232.     if ( this->ValidateStorageUnitView() )
  233.     {
  234.         TRY
  235.             Environment* ev = somGetGlobalEnvironment();
  236.         
  237.             char    padByte = 0x00;
  238.             long    curSize = fView->GetSize(ev);
  239.             long    curPosition = fView->GetOffset(ev);
  240.             
  241.             fView->SetOffset(ev, curSize);
  242.             
  243.             for (int i = 0; i < count; i++)
  244.             {
  245.                 StorageUnitViewSetValue(fView, ev, 1, &padByte);
  246.             }
  247.         
  248.             fView->SetOffset(ev, curPosition);
  249.         CATCH_ALL
  250.             // We can't throw from a Textension class.
  251.             return ErrorCode();
  252.         ENDTRY
  253.         
  254.         return noErr;
  255.     }
  256.     
  257.     return kODErrInvalidStorageUnit;
  258. }
  259.     
  260. //------------------------------------------------------------------------------
  261. // SUValueStream::Empty
  262. //------------------------------------------------------------------------------
  263.  
  264. void SUValueStream::Empty()
  265. {
  266.     if ( this->ValidateStorageUnitView() )
  267.     {
  268.         TRY
  269.             Environment* ev = somGetGlobalEnvironment();
  270.             fView->SetOffset(ev, 0);
  271.             fView->DeleteValue(ev, fView->GetSize(ev));
  272.         CATCH_ALL
  273.             // We can't throw from a Textension class.
  274.         ENDTRY
  275.     }
  276. }
  277.  
  278. //------------------------------------------------------------------------------
  279. // SUValueStream::Load
  280. //------------------------------------------------------------------------------
  281.  
  282. OSErr SUValueStream::Load(long size, Ptr* data)
  283. {
  284.     if ( this->ValidateStorageUnitView() )
  285.         return noErr;
  286.     
  287.     return kODErrInvalidStorageUnit;
  288. }
  289.     
  290. //------------------------------------------------------------------------------
  291. // SUValueStream::Unload
  292. //------------------------------------------------------------------------------
  293.  
  294. void SUValueStream::Unload(Ptr data)
  295. {
  296.     if ( this->ValidateStorageUnitView() )
  297.     {
  298.     }
  299. }
  300.         
  301. //------------------------------------------------------------------------------
  302. // SUValueStream::GetStorageUnit
  303. //------------------------------------------------------------------------------
  304.  
  305. ODStorageUnit *SUValueStream::GetStorageUnit()
  306. {
  307.     if ( this->ValidateStorageUnitView() )
  308.     {
  309.         TRY
  310.             Environment* ev = somGetGlobalEnvironment();
  311.             return fView->GetStorageUnit(ev);
  312.         CATCH_ALL
  313.             // We can't throw from a Textension class.
  314.         ENDTRY
  315.     }
  316.     
  317.     return nil;
  318. }
  319.         
  320. //------------------------------------------------------------------------------
  321. // SUValueStream::ValidateStorageUnitView
  322. //------------------------------------------------------------------------------
  323.  
  324. Boolean SUValueStream::ValidateStorageUnitView() const
  325. {
  326.     if ( !fView )
  327.         return false;
  328.     
  329.     return true;
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.